#include "drv_temperature.h"
#include <stdio.h>
tp tp1;
void drv_temperature_init(void)
{
	drv_iic_init();
}
uint16_t drv_temperature(void)
{
	uint16_t data=0;
	drv_iic_start();
	drv_iic_senddata(0x80);
	if(drv_iic_waitACK()!= ACK)
	{
		drv_iic_stop();
		return NACK;
	}
	
	drv_iic_senddata(0xe3);
	if(drv_iic_waitACK()!= ACK)
	{
		drv_iic_stop();
		return NACK;
	}
	
	drv_iic_start();
	drv_iic_senddata(0x81);
	if(drv_iic_waitACK()!= ACK)
	{
		drv_iic_stop();
		return NACK;
	}
	
	drv_systick_ms(85);
	data=drv_iic_readdata(ACK);
	data=data<<8;
	data |=drv_iic_readdata(NACK);
	drv_iic_stop();
	return data;
}
uint8_t drv_temperature_reset(void)
{
	drv_iic_start();
	drv_iic_senddata(0x80);
	if(drv_iic_waitACK() != ACK)
	{
		drv_iic_stop();
		return NACK;
	}
	drv_iic_senddata(0xfe);//复位命令
	if(drv_iic_waitACK() != ACK)
	{
		drv_iic_stop();
		return NACK;
	}
	drv_iic_stop();
	return 0;
}
tp drv_temperature_getdata(void)
{
	uint16_t temp=0;
	temp=drv_temperature();
	tp1.temp=-46.85+175.72*((double)temp/65536);
	tp1.left_1=(uint16_t)tp1.temp / 10;
	tp1.left_2=(uint16_t)tp1.temp % 10;
	tp1.left_3=((uint16_t)(tp1.temp*10) )%10;
	tp1.left_4=((uint16_t)(tp1.temp*100) )%10;
	return tp1;
}

